﻿namespace HCRS.Implementation
{
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Web;

    /// <summary>
    /// QQIP地址
    /// </summary>
    public class QQIPAddress
    {
        private static string ChangeIP(string IP)
        {
            try
            {
                IP = "0000000" + Convert.ToString(Convert.ToInt32(IP), 2);
                return IP.Substring(IP.Length - 8, 8);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 获取当前ＩＰ信息
        /// </summary>
        /// <returns></returns>
        public static IPAddress GetAddress()
        {
            return GetAddress(HttpContext.Current.Request.UserHostAddress.ToString());
        }

        /// <summary>
        /// 获取指定ＩＰ信息
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        public static IPAddress GetAddress(string IP)
        {
            IPAddress address = new IPAddress {
                IP = IP
            };
            OleDbConnection selectConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath + @"\App_Data\#QQ#;Persist Security Info=False");
            try
            {
                uint num = getIP(IP);
                DataTable dataTable = new DataTable();
                OleDbDataAdapter adapter = new OleDbDataAdapter(string.Concat(new object[] { "SELECT country,city from [ip_address] where [ip1]<=", num, " and ", num, "<=[ip2]" }), selectConnection);
                selectConnection.Open();
                adapter.Fill(dataTable);
                selectConnection.Close();
                if ((dataTable != null) && (dataTable.Rows.Count > 0))
                {
                    address.Country = dataTable.Rows[0][0].ToString();
                    address.City = dataTable.Rows[0][1].ToString().Replace("对方和您在同一", "");
                    address.Address = address.Country + address.City;
                    return address;
                }
                return address;
            }
            catch (Exception)
            {
                if (selectConnection.State == ConnectionState.Open)
                {
                    selectConnection.Close();
                }
                return address;
            }
        }

        private static uint getIP(string IP)
        {
            try
            {
                string str = "";
                string[] strArray = IP.Split(new char[] { '.' });
                if (strArray.Length < 4)
                {
                    return 0;
                }
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (Convert.ToInt32(strArray[i]) > 0xff)
                    {
                        return 0;
                    }
                    if (ChangeIP(strArray[i]) == null)
                    {
                        return 0;
                    }
                    str = str + ChangeIP(strArray[i]);
                }
                return Convert.ToUInt32(Convert.ToUInt32(str, 2).ToString(), 10);
            }
            catch (Exception)
            {
                return 0;
            }
        }
    }
}

